package online.longzipeng.householdbillssystem.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import online.longzipeng.householdbillssystem.business.dto.UserLoginResponseDTO;
import online.longzipeng.householdbillssystem.business.dto.user.UserQueryResponseDTO;
import online.longzipeng.householdbillssystem.business.entity.Bill;
import online.longzipeng.householdbillssystem.business.vo.bill.BillSaveVO;
import online.longzipeng.householdbillssystem.business.vo.bill.BillUpdateVO;
import online.longzipeng.householdbillssystem.business.vo.bill.QueryBillVO;
import online.longzipeng.householdbillssystem.business.vo.querey.QueryVO;
import online.longzipeng.householdbillssystem.common.result.ResponseEnum;
import online.longzipeng.householdbillssystem.common.result.Result;
import online.longzipeng.householdbillssystem.common.utils.ExceptionUtils;
import online.longzipeng.householdbillssystem.config.JWTConfig;
import online.longzipeng.householdbillssystem.service.BillService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lzp
 * @since 2021-04-17
 */
@RestController
@RequestMapping("/bill")
@Api(value = "账单相关接口",description = "账单相关接口",tags = "账单相关接口")
@Slf4j
public class BillController {

    @Autowired
    private BillService billService;
    @Autowired
    private JWTConfig jwtConfig;

    @PostMapping("/get-list-page")
    @ApiOperation("查询所有账单带分页")
    public Result getListPage(@RequestBody QueryBillVO queryVO,@RequestHeader("X-Token")String token){
        UserLoginResponseDTO userLoginResponseDTO = jwtConfig.checkJwt(token);
        IPage<Bill> bills = billService.getListPage(queryVO,userLoginResponseDTO.getId());
        return Result.success(bills);
    }

    @PostMapping("/save")
    @ApiOperation("新建保存账单信息")
    public Result save(@Valid @RequestBody BillSaveVO billSaveVO){
        Bill bill = new Bill();
        BeanUtils.copyProperties(billSaveVO,bill);
        ExceptionUtils.isTrue(billService.add(bill),"账单保存失败！！请检查填写的账单信息",bill);
        if (log.isDebugEnabled()){
            log.info(String.format("新建账单成功,账单信息：%s",bill.toString()));
        }
        return new Result(ResponseEnum.SUCCESS);
    }

    @DeleteMapping("/delete_one/{id}")
    @ApiOperation("删除单个账单信息")
    public Result deleteOne(@ApiParam(value="账单id",required = true) @PathVariable("id") Integer id){
        ExceptionUtils.isTrue(billService.delete(id),"删除失败！！参数不合法或已被其他用户删除",id);
        if (log.isDebugEnabled()){
            log.info(String.format("账单删除成功,账单信息：%d",id));
        }
        return new Result(ResponseEnum.SUCCESS);
    }

    @PostMapping("/delete")
    @ApiOperation("删除多个账单信息")
    public Result delete(@ApiParam(value="账单id数组",required = true, example = "1,2,3,4") @RequestParam("ids") Integer[] ids){
        for (Integer id : ids) {
            ExceptionUtils.isTrue(billService.delete(id),"删除失败！！参数不合法或已被其他用户删除",ids);
        }
        if (log.isDebugEnabled()){
            log.info(String.format("账单批量删除成功，ids：%s",ids.toString()));
        }
        return new Result(ResponseEnum.SUCCESS);
    }

    @PutMapping("/update")
    @ApiOperation("修改账单信息")
    public Result update(@Valid @RequestBody BillUpdateVO billUpdateVO){
        Bill bill = new Bill();
        BeanUtils.copyProperties(billUpdateVO,bill);
        ExceptionUtils.isTrue(billService.updateById(bill),"修改账单信息失败，请检查填写的信息",bill);
        if (log.isDebugEnabled()){
            log.info(String.format("修改账单信息成功，账单信息：%s",bill.toString()));
        }
        return new Result(ResponseEnum.SUCCESS);
    }

    @GetMapping("/find_from_group/{id}")
    @ApiOperation("查询用户组下的所有的账单")
    public Result findFromGroupByUserId(@ApiParam(value = "用户组id",required = true) @PathVariable("id") Integer id){
        List<Bill> bills = billService.findFromGroupByUserGroupId(id);
        Result result = new Result(ResponseEnum.SUCCESS);
        result.setData(bills);
        return result;
    }

    @GetMapping("/find_from_user/{id}")
    @ApiOperation(value = "查询当前用户私有的账单",notes = "用户自己创建的没有添加进用户组的账单")
    public Result findFromUserByUserId(@ApiParam(value = "用户id",required = true) @PathVariable("id") Integer id){
        List<Bill> bills = billService.findFromUserByUserId(id);
        Result result = new Result(ResponseEnum.SUCCESS);
        result.setData(bills);
        return result;
    }
}